package display 
{
	import controller.Controller;
	import data.PresetData;
	import events.DisplayEvent;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	
	public class DisplayMenu extends MovieClip 
	{
		private var control:Controller;
		
		private var type:String;
		private var bg:Sprite;
		private var itemAlpha:Number = 1;
		private var presetData:PresetData;
		private var controller:Controller;
		
		private var myColor:ColorTransform = new ColorTransform;
		
		private var holder:Sprite;
		private var tapbutton:Sprite;
		private var textField:DisplayTextField;
		
		private var colorOne:uint = 0xcccccc;
		private var colorTwo:uint = 0xffffff;
		
		private var selectedColorOne:uint = 0x000000;
		private var selectedColorTwo:uint = 0xCCCCCC;
		
		private var fontColor:uint = 0x000000;
		private var activeFontColor:uint = 0x000000;
		private var font:String = 'verdana';
		
		private var moduleBackGroundColor:uint = 0x000000;
		private var moduleAlpha:Number  = 1;
		private var mSpriteHolder:Sprite
		
		public function DisplayMenu(ctr:Controller) 
		{
			control = ctr;
			
			control.addEventListener(DisplayEvent.COLOR_CHANGED, onColorChanged);
			control.addEventListener(DisplayEvent.FONT_CHANGED, onFontChanged);
			control.addEventListener(DisplayEvent.ALPHA_CHANGED, onAlphaChanged);
			control.addEventListener(DisplayEvent.VISIBILITY_CHANGED, onVisibleChanged);
			
			createElements();
		}
		
		private function onVisibleChanged(e:DisplayEvent):void
		{
			if (e.data.item == 'Menu')
			{
				this.visible = e.data.selected;
			}
		}
		
		private function createElements():void
		{
			holder = new Sprite;
			holder.x = 0;
			holder.y = 30;
			
			mSpriteHolder = new Sprite;
	
			addChild(mSpriteHolder);
			addChild(holder);
			
			createButtons();
		}
		
		private function build():void
		{
			
		}
		
		private function clearHolder():void
		{
			while (holder.numChildren != 0)
			{
				holder.removeChildAt(0);
			}
		}
		
		private function createButtons():void
		{
			clearHolder();
			
			var matr:Matrix = new Matrix();
			var index:int = 0;
			var displayHighLight:DisplayHighLight;
			var tapHightLight:DisplayHighLight;
			var tapSelectedHighLight:DisplayHighLight;
			
			for (var i:int = 0; i < 3; i++)
			{
				//matrix
				matr.createGradientBox(78,30, Math.PI/2, 0, 0);
				
				//sprite
				var sprMethod:String = SpreadMethod.PAD;
				
				tapbutton = new Sprite;
				
				var buttonGradient:DisplayHighLight;
				
				if (index == 0)
				{
					tapbutton.graphics.beginGradientFill('linear', [selectedColorOne, selectedColorTwo], [1, 1], [0, 255], matr, sprMethod);
					tapbutton.graphics.drawRoundRectComplex(0, 0, 118, 30, 8, 8, 0, 0);
					
					
					displayHighLight = new DisplayHighLight( control, 'activeFontColor', 30, 118);
					textField = new DisplayTextField(font, activeFontColor);
				}
				else
				{
					tapbutton.graphics.beginGradientFill('linear', [colorOne, colorTwo], [1, 1], [0, 255], matr, sprMethod);
					tapbutton.graphics.drawRoundRectComplex(0, 0, 118, 28, 8, 8, 0, 0);
					
					buttonGradient = new DisplayHighLight(control, 'buttonGradient', 28, 118);
					displayHighLight = new DisplayHighLight( control, 'fontColor', 28, 118);
					textField = new DisplayTextField(font, fontColor);
				}
				
				tapbutton.graphics.endFill();
				tapbutton.x =  index * 120;
				
				//textfield
				textField.x = 10;
				textField.y = 8;
				
				
				tapbutton.addChild(displayHighLight);
				tapbutton.addChild(textField);
				
				if (buttonGradient)
				tapbutton.addChild(buttonGradient);
				
				
				
				
				holder.addChild(tapbutton);
				
				index++;
			}
			
				tapHightLight = new DisplayHighLight( control, 'tapButtonSelectedFontColor', 30, 118, 0, 0);
				tapSelectedHighLight = new DisplayHighLight( control, 'tapButtonSelectedGradient', 30, 118, 0, 0);
				
				holder.addChild(tapHightLight);
				holder.addChild(tapSelectedHighLight);
		}
		
		private function createModuleBackground():void
		{
			//clear
			if (mSpriteHolder.numChildren > 0)
			{
				mSpriteHolder.removeChildAt(0);
			}
			
			var mSprite:Sprite = new Sprite;
			
			
			mSprite.graphics.beginFill(moduleBackGroundColor,moduleAlpha);
			mSprite.graphics.drawRect(0, 60, 785,30);
			mSprite.graphics.endFill();
			
			
			var bgHighLight:DisplayHighLight = new DisplayHighLight(control, 'moduleBackgroundColor', 30,785, 0, 60);
			
			addChild(bgHighLight);
			mSpriteHolder.addChild(mSprite);
			
		}
		
		private function changeColor():void
		{
			createButtons();
		}
		
		private function onColorChanged(e:DisplayEvent):void
		{
			switch(e.data.itemCategory)
			{
				case 'tapButtonSelectedGradient':
				selectedColorOne = e.data.colorOne;
				selectedColorTwo = e.data.colorTwo;
				
				createButtons();
				break;
				
				case 'buttonGradient':
				colorOne = e.data.colorOne;
				colorTwo = e.data.colorTwo;
				
				createButtons();
				break;
				
				case 'moduleBackgroundColor':
				moduleBackGroundColor = e.data.colorOne;
				
				createModuleBackground();
				break;
				
				case 'activeFontColor':
				activeFontColor = e.data.colorOne;
				
				createButtons();
				break;
				
				case 'fontColor':
				fontColor = e.data.colorOne;
				
				createButtons();
				createModuleBackground();
				break;
			}
		}
		
		private function onFontChanged(e:DisplayEvent):void
		{
			font = e.data.font;
			
			createButtons();
		}
		
		private function onAlphaChanged(e:DisplayEvent):void
		{
			switch(e.data.itemCategory)
			{
				case 'moduleBackgroundAlpha':
				moduleAlpha = e.data.alpha / 100;
				
				createModuleBackground();
				break;
			}
		}
		
	}

}